﻿using MediatR;
using SystemLYH.API.Application.Command.LocationCommand;
using SystemLYH.Domain;
using SystemLYH.Domain.Dto;
using SystemLYH.Infrstructre.Interface;

namespace SystemLYH.API.Application.Handlers.LocationHandlers
{
    public class LocationShowHandlers : IRequestHandler<LocationShowCommand, PageDTO<Location>>
    {
        protected readonly ILocationRepository _locationRepository;
        /// <summary>
        /// 库位构造函数
        /// </summary>
        /// <param name="locationRepository"></param>
        public LocationShowHandlers(ILocationRepository locationRepository)
        {
            _locationRepository = locationRepository;
        }
        public async Task<PageDTO<Location>> Handle(LocationShowCommand request, CancellationToken cancellationToken)
        {
            PageDTO<Location> page = new PageDTO<Location>();
            var list = (await _locationRepository.GetAllAsync()).Where(x => !x.IsDelLocation);
            //库区名称
            if (request.LocationName != null)
            {
                list = list.Where(x => x.LocationName == request.LocationName);
            }
            //库区编号
            if (request.LocationNumber != null)
            {
                list = list.Where(x => x.LocationNumber == request.LocationNumber);
            }
            //库区状态
            if (request.LocationState != null)
            {
                list = list.Where(x => x.LocationState == request.LocationState);
            }
            page.pageRoot = list.Count();
            page.pageCount = (int)Math.Ceiling(page.pageRoot * 1.0 / request.pageSize);
            page.plist = list.OrderByDescending(x => x.LocationId).Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();
            return page;
        }
    }
}
